package layers;

import gui.Window;

import java.awt.Color;

import operations.Lifo;

import com.bbn.openmap.layer.DemoLayer;
import com.bbn.openmap.omGraphics.OMAction;
import com.bbn.openmap.omGraphics.OMGraphic;
import com.bbn.openmap.omGraphics.OMGraphicList;
import com.bbn.openmap.omGraphics.OMPoint;
import com.bbn.openmap.omGraphics.OMPoly;

import components.Node;
import components.NodeList;

public class NodeLayer extends DemoLayer{
	
	private NodeList nodes;
	private OMGraphic selectedGraphic;
	private Window window;
	
	public NodeLayer(NodeList nodes, Window window){
		super();
		
		this.selectedGraphic = null;
		this.nodes = nodes;
		this.window = window;
	}

	public OMGraphicList init(){
		 OMGraphicList omList = new OMGraphicList();
		 
		 Node node = null;
		 
		for (int i = 0; i < nodes.getCount(); i++) {
			node = nodes.getNodes().get(i);
			omList.add(createPoint(node.getLattitude(), node.getLongtitude(),
					node.getFour()));
		}
		 
		 return omList;
	}

	protected OMPoint createPoint(float lattitude, float longtitude, int four) {
		OMPoint point = new OMPoint(lattitude,longtitude);
		point.setFillPaint(Color.red);
		point.setSelectPaint(Color.orange);
		return point;
	}
	
	public void addNode(Node newNode) {
		this.getList().add(this.createPoint(newNode.getLattitude(), newNode.getLongtitude(), 0));
	}

	public void changeSelectedGraphic(Node node) {
		selectedGraphic = this.getSelected().getOMGraphicAt(0);
		if(selectedGraphic != null){
			selectedGraphic = createPoint(node.getLattitude(), node.getLongtitude(), node.getFour());
		}
		repaint();
	}
	
	public void drawingComplete(OMGraphic omg, OMAction action){
		super.drawingComplete(omg, action);
		
		if(action.getValue() == 128){
			window.getSW().addNewEvent(new Lifo(omg, Lifo.editNode));
		}
		else if(action.getValue() == 64){
			window.getSW().addNewEvent(new Lifo(omg, Lifo.delNode));
		}
		else if(action.getValue() == 4){
			window.getSW().addNewEvent(new Lifo(omg, Lifo.addNode));
		}
	}
	
	public int findGraphicPosition(OMPoint point){
		int pos = -1;
		for(int i=0; i<this.getList().size(); i++){
			if((OMPoint)this.getList().getOMGraphicAt(i) == point){
				pos = i;
				break;
			}
		}
		return pos;
	}
	
}
